package algorithm.queen;


import java.util.Stack;

/**
 * 使用两个栈实现队列的add、poll、push
 * Created by zhy on 2017/9/2.
 */
public class Main {
    private Stack<Integer> stackPush ;
    private Stack<Integer> stackPop ;

    public Main() {
        this.stackPush = new Stack<Integer>();
        this.stackPop = new Stack<Integer>();
    }

    public void add(int pushInt){
        stackPush.push(pushInt);
    }

    public int poll(){
        if(stackPush.empty() && stackPop.empty()){
            throw new RuntimeException("Queen is empty!");
        }else if(stackPop.empty()){//只有在队列为空时才可以入队
            while(!stackPush.empty()){
                stackPop.push(stackPush.pop());
            }
        }
        return stackPop.pop();
    }

    public int peak(){
        if(stackPush.empty() && stackPop.empty()){
            throw new RuntimeException("Queen is empty!");
        }else if(stackPop.empty()){
            while(!stackPush.empty()){
                stackPop.push(stackPush.pop());
            }
        }
        return stackPop.peek();
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.add(41);
        main.add(2);
        main.add(1);
        main.add(3);
        System.out.println(main.peak());
    }

}
